//
//  MyFighterDetailVC.m
//  u76ho
//
//  Created by m-air-01 on 15/12/28.
//  Copyright © 2015年 LV. All rights reserved.
//

#import "MyFighterDetailVC.h"
#import "MyFighterDetailFirCell.h"
#import "MyFighterDetailSecCell.h"
#import "MyFighterDetailThridCell.h"
#import "MyFighterDetailFourthCell.h"
#import "FriendImpressionVC.h"
#import "ContactPeopleDetailModel.h"
#import "InServerCompangyVC.h"
@interface MyFighterDetailVC ()
<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * _tableView;
    
    ContactPeopleDetailModel * myModel;
    int totalW;
}
@property (strong, nonatomic) FeRollingLoader *rollingLoader;
@end

@implementation MyFighterDetailVC
#pragma mark - 视图的生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
    
    _rollingLoader = [[FeRollingLoader alloc] initWithView:self.view title:@"LOADING"];
    [self.view addSubview:_rollingLoader];
    
    [_rollingLoader show];
    self.navigationItem.title =  @"详细资料";
    //[self addBackItem];
    // Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)main
{
    [self.view setBackgroundColor:[UIColor whiteColor]];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerNib:[UINib nibWithNibName:@"MyFighterDetailFirCell" bundle:nil] forCellReuseIdentifier:@"MyFighterDetailFirCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"MyFighterDetailSecCell" bundle:nil] forCellReuseIdentifier:@"MyFighterDetailSecCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"MyFighterDetailThridCell" bundle:nil] forCellReuseIdentifier:@"MyFighterDetailThridCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"MyFighterDetailFourthCell" bundle:nil] forCellReuseIdentifier:@"MyFighterDetailFourthCell"];
    _tableView.backgroundColor = UIColorFromRGB(0xf1f3f3);

    [self.view addSubview:_tableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self requestData];
}
-(void)requestData
{
    [U76HONet mineContactPeopleWithAction:@"" code:[Singleton share].code code1:_friendCode andDataBlock:^(ContactPeopleDetailModel *model) {
        NSLog(@"我的联系人详情获取数据成功了");
        myModel = model;
        [_tableView reloadData];
        [_rollingLoader removeFromSuperview];
    }];
}
#pragma mark - _tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        {
            MyFighterDetailFirCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyFighterDetailFirCell" forIndexPath:indexPath];
            if (myModel!=nil) {
                cell.baseInfomodel = myModel.baseInfomodel;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case 1:
        {
            MyFighterDetailSecCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyFighterDetailSecCell" forIndexPath:indexPath];
            if (myModel!=nil) {
                cell.baseInfomodel = myModel.baseInfomodel;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case 2:
        {
            MyFighterDetailThridCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyFighterDetailThridCell" forIndexPath:indexPath];
            if (myModel!=nil) {
                cell.dataArr = myModel.friendImpressionModelArray;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
        case 3:
        {
            MyFighterDetailFourthCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyFighterDetailFourthCell" forIndexPath:indexPath];
            if (myModel!=nil) {
                cell.picArr = myModel.friendDynaModelArray;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
    }
    return nil;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self)weakSelf = self;
    if (indexPath.row == 2) {
        FriendImpressionVC * friendImpresstionVC = [[FriendImpressionVC alloc]init];
        friendImpresstionVC.friendCode = _friendCode;
        [friendImpresstionVC setFriendImpressBlock:^{
            [weakSelf requestData];
        }];
        [self.navigationController pushViewController:friendImpresstionVC animated:YES];
    }
    else if (indexPath.row == 3)
    {
        InServerCompangyVC * inscVC = [[InServerCompangyVC alloc]init];
        inscVC.isMydyna = @"mine";
        inscVC.code = _friendCode;
        [self.navigationController pushViewController:inscVC animated:YES];

    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 150;
    }
    else if (indexPath.row == 1)
    {
        return 75;
    }
    else if (indexPath.row == 2)
    {
        if (myModel.friendImpressionModelArray.count!=0) {
            int j = 0;
            totalW = 0;
            NSMutableArray * mArr = [[NSMutableArray alloc]init];
            for (int i = 0; i<myModel.friendImpressionModelArray.count; i++) {
                friendImpressionModel * model = myModel.friendImpressionModelArray[i];
                [mArr addObject:model.content];
            }
            //    CGFloat h = (self.frame.size.height - SIZETO6H(16)*2-SIZETO6H(10))/2;
            CGFloat h = 30;
            CGFloat myWidth = SCREEN_WIDTH - SIZETO6W(70);
            for (int i = 0; i<mArr.count; i++) {
                NSString * str = [mArr objectAtIndex:i];
                CGFloat tempW = [str widthOfSize:18] + SIZETO6W(10);
                totalW += tempW+SIZETO6W(10);
                if (totalW<=myWidth) {
                    
                }
                else
                {
                    totalW = tempW+SIZETO6W(10);
                    j = j+1;
                    if (j == 2) {
                        break;
                    }
                    
                }
            }
            return 38 + 5 + h + j*(h+SIZETO6H(10))+5;

        }
        return 102;
    }
    else
    {
        return 96;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

-(void)backItem:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
